﻿using log4net;
using log4net.Appender;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Gl_mes.Tools
{
    public class Log4net
    {
        public class LogHelper
        {
            //private Dictionary<string, ILog> LogDic = new Dictionary<string, ILog>();
            //private object _islock = new object();
            //private string fileName = string.Empty;

            private static readonly ILog logInfo = LogManager.GetLogger("Log");
            private static readonly ILog logErr = LogManager.GetLogger("Err");


            ///// <summary>
            ///// 日志调用初始化
            ///// </summary>
            ///// <param name="fileSavePath">日志文件保存路径[若路径为空，则默认程序根目录Logger文件夹;]</param>
            ///// <param name="fileName">日志文件名[若文件名为空，则默认文件名：Default]</param>
            //public LogHelper(string fileSavePath, string fileName, string logSuffix = ".log")
            //{
            //    try
            //    {
            //        Init();
            //        if (string.IsNullOrEmpty(fileSavePath))
            //            fileSavePath = "Logger";
            //        if (string.IsNullOrEmpty(fileName))
            //            fileName = "Default";

            //        this.fileName = fileName;
            //        var repository = LogManager.GetRepository();
            //        var appenders = repository.GetAppenders();

            //        if (appenders.Length == 0) return;
            //        var targetApder = appenders.First(p => p.Name == "ErrorAppender") as RollingFileAppender;
            //        targetApder.File = Path.Combine(fileSavePath, this.fileName + logSuffix);
            //        targetApder.ActivateOptions();
            //    }
            //    catch (Exception ex) { }
            //}


            ///// <summary>
            ///// 日志记录初始化
            ///// </summary>
            //private void Init()
            //{
            //    var file = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log4net.config"));
            //    XmlConfigurator.Configure(file);
            //}

            /// <summary>
            /// 记录正常的消息
            /// </summary>
            /// <param name="msg">消息内容</param>
            public static void info(string msg)
            {
                logInfo.Info(msg);
            }

            /// <summary>
            /// 记录异常信息
            /// </summary>
            /// <param name="msg">异常信息内容</param>
            public static void error(string msg)
            {
                StackTrace stackTrace = new StackTrace();
                StackFrame stackFrame = stackTrace.GetFrame(1);
                MethodBase methodBase = stackFrame.GetMethod();
                logErr.Error("类名:" + methodBase.ReflectedType.Name + " 方法名:" + methodBase.Name + " 信息:" + msg);
            }
        }
    }
}
